#include "TCP_server.hpp"
#include <stdlib.h>

int  Server_Init(TCP* tvr,std::string ip,int port)
{
    if(tvr->Init())
    {
        if(tvr->Bind(ip,port))
        {
            if(tvr->Listen())
            {
                return 0;
            }
            else
            {
                cout<<"FATAL Error In Listen \n"; 
                return -4;
            }
        }
        else
        {
            cout<<"FATAL Error In Bind \n";
            return -3;
        }
    }
    else 
    {
        cout<<"FATAL Error In Init \n";
        return -2;
    }
    return 0;
}

int main(int argc ,char* argv[])
{
    if(argc != 3)
    {
        cout<<"./server IP PORT is right style"<<endl;
        return -1;
    }
    TCP* tvr=new TCP();
    std::string ip=argv[1];
    int port=atoi(argv[2]);
    if(Server_Init(tvr,ip,port) >=0)
    {
        tvr->Start();
    }
    return 0;
}

